<?php

/**
 * This is the model class for table "mms_project".
 *
 * The followings are the available columns in table 'mms_project':
 * @property integer $projectID
 * @property string $studentID
 * @property string $projectName
 * @property string $projectEngName
 * @property string $active
 * @property string $startDate
 * @property string $endDate
 * @property string $statusCode
 * @property string $approve
 * @property string $year
 * @property string $semester
 * @property integer $projectCredit
 * @property string $createUser
 * @property string $createDate
 * @property string $modifyUser
 * @property string $modifyDate
 *
 * The followings are the available model relations:
 * @property Document[] $documents
 * @property Exam[] $exams
 * @property Student $student
 * @property ProjectStatus[] $projectStatuses
 * @property Teacher[] $mmsTeachers
 */
class Project extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'mms_project';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('studentID,year,semester,projectName,startDate', 'required','message'=>'กรุณากรอกข้อมูล {attribute}'),
			array('projectID, projectCredit', 'numerical', 'integerOnly'=>true),
			array('studentID, createUser, modifyUser', 'length', 'max'=>20),
			array('projectName, projectEngName', 'length', 'max'=>512),
			array('active, approve, year, semester', 'length', 'max'=>4),
			array('startDate, endDate, createDate, modifyDate', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('projectID, studentID, projectName, projectEngName, active, startDate, endDate, statusCode, approve, year, semester, projectCredit, createUser, createDate, modifyUser, modifyDate', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function scopes() {
		return array(
				'today'=>array(
						'condition'=>'date >= NOW()',
				),
		);
	}
	public function behaviors(){
		  return array( 'EAdvancedArBehavior' => array(
	            'class' => 'application.extensions.EAdvancedArBehavior',
	      ));
	}
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'documents' => array(self::HAS_MANY, 'Document', 'projectID'),
			'exams' => array(self::HAS_MANY, 'Exam', 'projectID'),
			'student' => array(self::BELONGS_TO, 'Student', 'studentID'),
			'projectStatuses' => array(self::HAS_MANY, 'ProjectStatus', 'projectID'),
			'lastStatus' => array(self::BELONGS_TO, 'Status', 'statusCode'),
			'teachers' => array(self::MANY_MANY, 'Teacher', 'mms_project_teacher(projectID, teacherID)'),
				'projectTeachers' => array(self::HAS_MANY, 'ProjectTeacher', 'projectID'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'projectID' => 'รหัสโครงการสารนิพนธ์',
			'studentID' => 'รหัสนักศึกษา',
			'projectName' => 'ชื่อโครงการสารนิพนธ์',
			'projectEngName' => 'ชื่อโครงการสารนิพนธ์ภาษาอังกฤษ',
			'active' => 'สถานะการเปิดใช้งาน',
			'startDate' => 'วันที่ยื่นโครงการสารนิพนธ์',
			'endDate' => 'วันเสร็จสิ้นโครงการสารนิพนธ์',
			'statusCode' => 'รหัสสถานะโครงการ',
			'approve' => 'การอนุมัติ',
			'year' => 'ปีการศึกษา',
			'semester' => 'ภาคการศึกษา',
			'projectCredit' => 'หน่วยกิตที่ลงทะเบียนสารนิพนธ์เทอมนี้',
			'createUser' => 'ผู้บันทึก',
			'createDate' => 'วันที่บันทึก',
			'modifyUser' => 'ผู้ปรับปรุงข้อมูล',
			'modifyDate' => 'วันที่ปรับปรุงข้อมูล',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search($type=null)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('projectID',$this->projectID);
		$criteria->compare('t.studentID',$this->studentID,true);
		$criteria->compare('projectName',$this->projectName,true);
		$criteria->compare('projectEngName',$this->projectEngName,true);
		$criteria->compare('t.active',$this->active,true);
		$criteria->compare('startDate',$this->startDate,true);
		$criteria->compare('endDate',$this->endDate,true);
		$criteria->compare('statusCode',$this->statusCode);
		$criteria->compare('approve',$this->approve,true);
		$criteria->compare('year',$this->year,true);
		$criteria->compare('semester',$this->semester,true);
		$criteria->compare('projectCredit',$this->projectCredit);
		$criteria->compare('createUser',$this->createUser,true);
		$criteria->compare('createDate',$this->createDate,true);
		$criteria->compare('modifyUser',$this->modifyUser,true);
		$criteria->compare('modifyDate',$this->modifyDate,true);
		
		//if (!$type) {
		$criteria->compare('regStudent.student_name',$this->student->regStudent->student_name,true);
		$criteria->with='student';
		$criteria->with='student.regStudent';
		$criteria->with='student.regStudent.prefix';
		//}
		if ($type=="teacher")
		{
			$criteria->join = "LEFT JOIN mms_project_teacher ON t.projectID=mms_project_teacher.projectID";
			//$criteria->with=array('projectTeachers');
			$criteria->compare('mms_project_teacher.teacherID',$this->teachers[0]->teacherID);
			
		}
		if ($type=="committee")
		{
			$criteria->join = "LEFT JOIN mms_exam ON t.projectID=mms_exam.projectID LEFT JOIN mms_committee on mms_exam.examID=mms_committee.examID";
			//$criteria->with=array('projectTeachers');
			$criteria->compare('mms_committee.teacherID',$this->exams[0]->committees[0]->teacherID);
				
		}
		
		
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Project the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
